quiet = true;

function mflog(msg) {
	if(quiet)
		console.log(msg);
	else
		alert(msg);
}


(function($) {

	$.extend($.fn, {
		
		sliders: function(optionsPassed) {
			
			function calculateBg(pos1, pos2) {
				var str = {
					left: pos1,
					width: (pos2-pos1)
				};
				if(options.bg_image != null)str.backgroundPosition = '-' + pos1 + 'px 0px';

				return str;
			}

			function pos2val(pos) {
				if(options.value_type == 'int') {
					return parseInt(pos * (options.max_value - options.min_value) / options.width + options.min_value );
				}else if(options.value_type == 'float') {
					var flost = parseFloat(pos * (options.max_value - options.min_value) / options.width + options.min_value  );
					flost = Math.round(flost * 2) / 2;
					flost = $.formatNumber(flost,{format:'######0.0',locale:'us'});



					return flost;
				}
				return 0;
			}

			function val2pos(val) {
				return parseInt(parseFloat(val - options.min_value) * options.width / (options.max_value - options.min_value));
			}

			var options = $.extend({
				//selector obiektu do którego łądujemy wartość(minimalną)
				min_obj: this,
				//selector obiektu do którego ładujemy wartość  maxymalną
				max_obj: null,
				//początek zakresu
				min_value: 0,
				//koniec zakresu
				max_value: 100,
				//nie obsłużone
				snap_to_grid: false,
				//nie obsłużone
				grid_density: 1,
				//prefix wyświetlanej wartości
				prefix: '',
				//postfix wyświetlanej wartości
				postfix: '',
				//label slidera
				label: 'Slider',
				////szerokość obszaru poruszania
				width: 240,
				//przesunięcie buttona względem punktu centralnego
				shift: -13,
				//typ wyliczanej wartości - może być jeszcze float
				value_type: 'int',
				//tło do podziałkli
				scale_image: null,
				//tło do podziałkli
				bg_image: null,

				mouse_up_handler: null 
			},optionsPassed);

			var min_obj = $(options.min_obj);
			var max_obj = (options.max_obj == null || options.max_obj == undefined)?null:$(options.max_obj);

			if(this.val() == undefined || this.val().length == 0){
				mflog('Minimal object has no value');
				return;
			}

			if(max_obj != null && (max_obj.val() == undefined || max_obj.val().length == 0)){
				mflog('Maximal object has no value');
				return;
			}

			var button1 = $('<a class="button1" id="button'+min_obj.attr('id')+'"></a>');
			var button1position = val2pos(this.val());
			button1.css({left: (button1position + options.shift)});
			button1.text(this.val()+options.postfix);

			bg = $('<div class="sliderBgV"></div>');
			bg.css(calculateBg(0,button1position));

			var button2;
			var bg;
			
			if(max_obj != null) {
				button2 = $('<a class="button2" id="button'+max_obj.attr('id')+'"></a>');
				var button2position = val2pos(max_obj.val());//parseInt(parseFloat(max_obj.val()) * options.width / (options.max_value - options.min_value));

				if(button2position < button1position) {
					mflog('Wrong values entered');
					return;
				}

				bg.css(calculateBg(button1position,button2position));

				button2.css({left: (button2position + options.shift)});
				button2.text(max_obj.val()+options.postfix);
			}

			var label = $('<p class="label">'+options.label+'</p>');
			var sliderBg = $('<div class="sliderBg"></div>');

			sliderBg.append(bg).append(button1).append(button2);

			var main = $('<div class="slider"></div>');

			main.append(label).append(sliderBg);

			if(options.scale_image != null) {
				var scale = $('<div></div>');
				scale.css({backgroundImage: "url("+options.scale_image+")", backgroundPosition: "top center", backgroundRepeat: "no-repeat"});
				scale.width(options.width);
				scale.height(21);
				main.append(scale);
			}



			this.before(main);

			if(options.bg_image != null) {
				bg.css({backgroundImage: "url("+options.bg_image+")", backgroundPosition: "top center", backgroundRepeat: "no-repeat"});
			}

			var _e = null;
			var _e2 = null;

			if(button2 != undefined && button2 != null)$('#button'+max_obj.attr('id'))
				.live('mousedown',function(){

				var fixer = $('<div class="fixer" id="fixer'+max_obj.attr('id')+'"></div>');
				fixer.css({width: $(document).width()-30, height: $(document).height(), backgroundColor: 'black', position: 'absolute', top: 0, left: 0, zIndex: 1000, filter: 'alpha(opacity=0)', opacity: 0});
				$('body').append(fixer);

				 _e = null;
				 return false;
			});

			if(max_obj != null) {

			$('#fixer'+max_obj.attr('id'))
				.live('mousemove',function(e){
					e.stopPropagation();
					if(_e == null) {
						_e = e;
					}

					var new_pos = button2position + e.pageX - _e.pageX;
//						var new_pos = button2position + (e.pageX - button2.offset().left);
					if(new_pos < button1position || new_pos > options.width) {
						return;
					}

					button2position = new_pos;
					button2.css({left: (button2position+options.shift)});
					bg.css(calculateBg(button1position,button2position));

					var new_val = pos2val(button2position);

					if(button2position > options.width - 5)new_val = pos2val(options.width);

					button2.text(options.prefix+''+new_val+''+options.postfix);
					max_obj.val(new_val);

					_e = e;

					return;
				})
				.live('mouseout',function(){
					_e = null;
					$('#fixer'+max_obj.attr('id')).remove();
				})
				.live('mouseup',function(){
					_e = null;
					$('#fixer'+max_obj.attr('id')).remove();
					if(options.mouse_up_handler)
					{
						options.mouse_up_handler();
					}					
					
					
				});
			}

			$('#'+button1.attr('id') + ', #'+button1.attr('id') + ' *')
				.live('mousedown',function(){

				
				var fixer = $('<div class="fixer" id="fixer'+min_obj.attr('id')+'"></div>');
				fixer.css({width: $(document).width()-30, height: $(document).height(), backgroundColor: 'white', position: 'absolute', top: 0, left: 0, zIndex: 1000, filter: 'alpha(opacity=0)', opacity: 0});
				$('body').append(fixer);

				_e2 = null;
				return false;
			});

			$('#fixer'+min_obj.attr('id'))
				.live('mousemove',function(e2){
					if(_e2 == null) {
						_e2 = e2;
					}
					e2.stopPropagation();
					var new_pos = button1position + e2.pageX - _e2.pageX;

					if(new_pos < 0 || new_pos > button2position || new_pos > options.width) {
						return;
					}

					button1position = new_pos;
					button1.css({left: (button1position+options.shift)});
					
					if(bg != undefined && bg != null && max_obj != null){
						bg.css(calculateBg(button1position,button2position));
					}else{
						bg.css({left: 0, width: button1position, backgroundPosition: '0 0'});
					}

					var new_val = pos2val(button1position);

					if(button1position < 5)new_val = pos2val(0);
					if(button1position > options.width - 5)new_val =  pos2val(options.width);

					button1.text(options.prefix+''+new_val+''+options.postfix);
					min_obj.val(new_val);

					_e2 = e2;

					return;
				})
				.live('mouseout',function(){
					_e2 = null;
					$('#fixer'+min_obj.attr('id')).remove();
				})
				.live('mouseup',function(){
					_e2 = null;
					$('#fixer'+min_obj.attr('id')).remove();
					
					if(options.mouse_up_handler)
					{
						options.mouse_up_handler();
					}
					
				});
			

			

		}

	});

})(jQuery);

